import {Component, ViewChild} from '@angular/core';
import {NavController, NavParams} from 'ionic-angular';

import {Backend} from '../../providers/backend';

import {TabEvents} from '../tab-events/tab-events';
import {TabNews} from '../tab-news/tab-news';
import {TabAbout} from '../tab-about/tab-about';
import {TabResources} from '../tab-resources/tab-resources';
import {TabMy} from '../tab-my/tab-my';

import {TabTestPage} from '../tab-test/tab-test';

@Component({
  templateUrl: 'build/pages/tabs/tabs.html',
  queries: {
    mainTabs: new ViewChild('mainTabs')
  }
})
export class TabsPage {
  static get parameters() {
    return [[NavController], [NavParams], [Backend]];
  }

  constructor(nav, params, backend) {
    this.nav = nav;
    this.params = params;
    this.backend = backend;

    this.tab1Root = TabEvents;
    this.tab2Root = TabNews;
    this.tab3Root = TabAbout;
    this.tab4Root = TabResources;
    this.tab5Root = TabMy;
    this.tab6Root = TabTestPage;
  }

  authCheck() {
    this.backend.ensureLogined(this.nav).then((result) => {
      if (!result) {
        this.mainTabs.select(this.mainTabs.previousTab());
      }
    });
  }

  goBack() {

    if(this.mainTabs.getSelected().canGoBack()) {
      this.mainTabs.getSelected().pop();
      return true;
    } else {
      if(this.mainTabs.getSelected().tabTitle == 'Events') {
        return false;
      }

      let tabPrevious = this.mainTabs.previousTab();

      if(!tabPrevious) {
        return false;
      }

      this.mainTabs.select(tabPrevious);
      return true;
    }
  }
}
